package bw.gui;

import bw.owl.Ontology;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.OutputStream;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
//import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;



import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;



class GUI extends Thread implements ActionListener {

    private JFrame frame;
    private InstanceViewer viewer;
    private JButton otworz;
    private JButton zapisz;
    private JButton zakoncz;

    public GUI() {
        try {
            SwingUtilities.invokeAndWait(this);
            Ontology.load(new File("./PracownicyZadaniaTerminy.owl"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run()     {
        frame = new JFrame("Pracownicy, zadania, umiejętności");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        addMenuBar();
        addInstanceViewer();

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private void addMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        otworz = addButton(menuBar, "Otwórz");
        zapisz = addButton(menuBar, "Zapisz");
        menuBar.add(new JMenuItem());
        zakoncz = addButton(menuBar, "Zakończ");

        frame.setJMenuBar(menuBar);

        otworz.addActionListener(this);
        zapisz.addActionListener(this);
        zakoncz.addActionListener(this);
    }

    private JButton addButton(JMenuBar menu, String buttonText) {
        JButton button = new JButton(buttonText);
        menu.add(button);
        return button;
    }

    private void addInstanceViewer() {
        viewer = new InstanceViewer();
        frame.add(viewer);
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == otworz) {

            JFileChooser fileChooser = new JFileChooser("./");
                  
            fileChooser.resetChoosableFileFilters();
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            FileNameExtensionFilter filter = new FileNameExtensionFilter("Pliki owl","owl");
            fileChooser.setFileFilter(filter);

            
            int returnVal = fileChooser.showOpenDialog(null);
        

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fileChooser.getSelectedFile();
                Ontology.load(file);
                viewer.update();
            }
        }
        else if(e.getSource() == zapisz) {

            JFileChooser fileChooser = new JFileChooser("./");
            int returnVal = fileChooser.showSaveDialog(null);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fileChooser.getSelectedFile();
                try {
                    OutputStream out = new FileOutputStream(file);
                    Ontology.getManager()
                    	.saveOntology(Ontology.getOntology()
                    		, new RDFXMLOntologyFormat(), out);
                }
                catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        else if(e.getSource() == zakoncz) {
            frame.dispose();
            System.exit(0);
        }
    }
}
